﻿Public Class xtf_LISTADOlistaprecio_

    Public Overrides Sub IniciarLista()
        Try
            gdc_general.BeginUpdate()
            gdc_general.DataSource = dtt_DataTable
            'ulg_Lista.DisplayLayout.GroupByBox.Style = Infragistics.Win.UltraWinGrid.GroupByBoxStyle.Full

            'Creacion y carga de Cuentas
            dtt_DataTable.Columns.Add("Identificador (ID)")
            dtt_DataTable.Columns.Add("Nombre de Lista")
            dtt_DataTable.Columns.Add("Razón Social")
            dtt_DataTable.Columns.Add("Descripción")
            dtt_DataTable.Columns.Add("Fecha")
            dtt_DataTable.Columns.Add("Estado")
            dtt_DataTable.Columns.Add("Tipo")

            gdv_general.PopulateColumns(dtt_DataTable)

            Dim obj_ListaPrecio_ As New cls_listaprecio_
            If obj_ListaPrecio_.CargarTodos(ObtenerRegistroEstado) Then
                For Each ListaPrecio As cls_listaprecio_ In obj_ListaPrecio_.Items
                    AgregarItem(ListaPrecio)
                Next
            End If
            'ulg_Lista.DataBind()

            FiltrarColumnas()
            gdc_general.EndUpdate()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Function AgregarItem(ByRef ListaPrecio As cls_listaprecio_) As DataRow
        Dim Row As DataRow = dtt_DataTable.NewRow
        Row("Identificador (ID)") = ListaPrecio.idlistaprecio
        Row("Nombre de Lista") = ListaPrecio.nombre
        If Not IsNothing(ListaPrecio.cliente) Then
            Row("Razón Social") = ListaPrecio.cliente.razonsocial
        Else
            Row("Razón Social") = ""
        End If
        Row("Descripción") = ListaPrecio.descripcion
        Row("Fecha") = ListaPrecio.fecha.Date
        Row("Estado") = ListaPrecio.estado
        Select Case ListaPrecio.tipo
            Case cls_listaprecio_.enu_tipo.Cliente
                Row("Tipo") = "Cliente"
            Case cls_listaprecio_.enu_tipo.General
                Row("Tipo") = "General"
        End Select


        dtt_DataTable.Rows.Add(Row)
        'ulg_Lista.Rows(ulg_Lista.Rows.Count - 1).Height = 19
        Return Row
    End Function

    Public Sub ModificarItem(ByRef ListaPrecio As cls_listaprecio_, ByRef ulg_row As DataRow)
        ulg_row("Identificador (ID)") = ListaPrecio.idlistaprecio
        ulg_row("Nombre de Lista") = ListaPrecio.nombre
        If Not IsNothing(ListaPrecio.cliente) Then
            ulg_row("Razón Social") = ListaPrecio.cliente.razonsocial
        Else
            ulg_row("Razón Social") = ""
        End If
        ulg_row("Descripción") = ListaPrecio.descripcion
        ulg_row("Fecha") = ListaPrecio.fecha.Date
        ulg_row("Estado") = ListaPrecio.estado
        Select ListaPrecio.tipo
            Case cls_listaprecio_.enu_tipo.Cliente
                ulg_row("Tipo") = "Cliente"
            Case cls_listaprecio_.enu_tipo.General
                ulg_row("Tipo") = "General"
        End Select
    End Sub
    Public Overrides Sub bbi_Nuevo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISONuevo) Then
            bsi_estado.Caption = "Cargando..."
            Application.DoEvents()
            Dim ABM As Object
            Dim parametro As New cls_config_parametro_
            parametro.Buscar("interfaz")
            If parametro.valores = "Produccion" Then
                ABM = CType(Activator.CreateInstance(Type.GetType(Replace(Application.ProductName, " ", "_") & "." & Strings.Left(Replace(Me.Name, "xtf_LISTADO", "xtf_ABM"), Replace(Me.Name, "xtf_LISTADO", "xtf_ABM").Length - 1), True, True)), Form)
            Else
                ABM = New xtf_ABMlistaprecio2
            End If
            ABM.Nuevo(Me)
            ABM.ControlarModificaciones(ABM.lac_general.items)
            bsi_estado.Caption = "Listo"
        End If
        Strings.Left(Replace(Me.Name, "xtf_LISTADO", "xtf_ABM"), Replace(Me.Name, "xtf_LISTADO", "xtf_ABM").Length - 2)
    End Sub

    Public Overrides Sub bbi_Modificar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOModificar) Then
            Try
                Dim barrio As Object 'New cls_barrio_
                barrio = Activator.CreateInstance(Type.GetType(Replace(Application.ProductName, " ", "_") & "." & Replace(Me.Name, "xtf_LISTADO", "cls_"), True, True))
                If gdv_general.SelectedRowsCount > 0 Then
                    If barrio.Buscar(gdv_general.GetDataRow(gdv_general.GetSelectedRows(0))("Identificador (ID)")) Then
                        bsi_estado.Caption = "Cargando..."
                        Application.DoEvents()
                        Dim ABM As Object 'New xtf_ABMbarrio
                        Dim parametro As New cls_config_parametro_
                        parametro.Buscar("interfaz")
                        If parametro.valores = "Produccion" Then
                            ABM = CType(Activator.CreateInstance(Type.GetType(Replace(Application.ProductName, " ", "_") & "." & Strings.Left(Replace(Me.Name, "xtf_LISTADO", "xtf_ABM"), Replace(Me.Name, "xtf_LISTADO", "xtf_ABM").Length - 1), True, True)), Form)
                        Else
                            ABM = New xtf_ABMlistaprecio2
                        End If
                        ABM.Modificar(barrio, Me, gdv_general.GetDataRow(gdv_general.GetSelectedRows(0)))
                        ABM.ControlarModificaciones(ABM.lac_general.items)
                        bsi_estado.Caption = "Listo"
                    End If
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub
    Public Sub New()

        Me.Cursor = Cursors.WaitCursor
        InitializeComponent()
        PERMISONuevo = "008006002"
        PERMISOModificar = "008006003"
        PERMISOEliminar = "008006004"
        PERMISOImprimir = "008006005"
        Me.Text = lbl_modulo.Text : lbl_modulodescripcion.Text = "Listado de " & lbl_modulo.Text & "."
        IniciarLista()
        Me.Cursor = Cursors.Default

    End Sub

    Public Overrides Sub bbi_imprimir_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOImprimir) Then
            Dim report As xtr_REPORTElistaprecio2 = generarReporte()
            Try
                report.Print()
            Catch ex As Exception
                ErrManagerShow("Error inesperado.", ex.Message, xtf_ErrManagerAlerta.errManagerImg.img_Error, xtf_ErrManagerAlerta.errManagerBtn.btn_SalirDelSistema)
            End Try
        End If
    End Sub

    Public Overrides Sub bbi_exportarPDF_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOImprimir) Then
            Dim report As xtr_REPORTElistaprecio2 = generarReporte()

            Dim SAVEFILE As New SaveFileDialog
            SAVEFILE.Filter = "PDF (*.PDF)|*.PDF"
            SAVEFILE.FilterIndex = 2
            If SAVEFILE.ShowDialog() = DialogResult.OK Then
                report.ExportToPdf(SAVEFILE.FileName)
            End If
        End If
    End Sub

    Public Overrides Sub bbi_vistaprevia_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOImprimir) Then
            Dim report As xtr_REPORTElistaprecio2 = generarReporte()
            report.ShowPreview()
        End If
    End Sub

    Public Overrides Sub bbi_exportarExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOImprimir) Then
            Dim report As xtr_REPORTElistaprecio2 = generarReporte()
            Dim SAVEFILE As New SaveFileDialog
            SAVEFILE.Filter = "XLS (*.XLS)|*.XLS"
            SAVEFILE.FilterIndex = 2
            If SAVEFILE.ShowDialog() = DialogResult.OK Then
                report.ExportToXls(SAVEFILE.FileName)
            End If
        End If
    End Sub

    Private Function generarReporte() As xtr_REPORTElistaprecio2
        Dim obj_command As New SqlClient.SqlCommand()
        Dim obj_Conexion As New cls_Conexion
        obj_Conexion.Conectar()
        With obj_command
            .CommandType = CommandType.StoredProcedure
            .Connection = obj_Conexion.obj_SqlConnection
            .CommandText = "listaprecioarticulo__reporte2"
            '.Parameters.Add("@idlistaprecio", SqlDbType.Int).Value = txt_ID.Text
        End With
        Dim obj_Adapter As New SqlClient.SqlDataAdapter(obj_command)
        Dim dts_Movimientos As New dts_listaprecio
        obj_Adapter.Fill(dts_Movimientos.Tables(1))

        Dim parametro As New cls_config_parametro_
        parametro.Buscar("rubroPrincipal")
        Dim rubro As New cls_articulorubro_
        If rubro.Buscar(parametro.valor) Then
            For Each row As DataRow In dts_Movimientos.Tables(1).Rows
                If row("articulorubro_nombre_ordenado") = rubro.nombre Then
                    row("articulorubro_nombre_ordenado") = "0 " & row("articulorubro_nombre_ordenado")
                End If
            Next
        End If

        Dim report As New xtr_REPORTElistaprecio2
        report.DataSource = dts_Movimientos

        obj_Conexion.Desconectar()

        Return report
    End Function
   
End Class